<script setup>
import { ref, onMounted } from 'vue'
import { useCartStore } from '@/stores/cartStore'
import { getAddressByIdAPI } from '@/apis/address'
import { pay4OrderAPI, getProductInfoAPI } from '@/apis/order'
import { useRouter, useRoute } from 'vue-router'

const cartStore = useCartStore()
const payMethod = ref(1)

const changePayType = (type) => {
  payMethod.value = type
}

const router = useRouter()
const pay = async () => {
  const res = await pay4OrderAPI({
    addrId: cartStore.address.id,
    orderIdList: cartStore.orderIdList,
    payMethod: payMethod.value
  })
  if (res.code === 2000) {
    cartStore.clearAll()
    ElMessage({ type: 'success', message: '支付成功' })
    router.go(-1)
  }
}

const route = useRoute()
onMounted(async () => {
  const addressId = route.query.addressId
  if (addressId) {
    const addressRes = await getAddressByIdAPI(addressId)
    cartStore.address = addressRes.data
  }
})
</script>

<template>
<div class="xtx-pay-checkout-page">
<div class="container">
    <div class="wrapper">
    <!-- 收货地址 -->
    <h3 class="box-title">收货地址</h3>
    <div class="address-card">
        <div class="address-info">
            <div class="info-item">
                <div class="label">收货人：</div>
                <div>{{ cartStore.address.consignee }}</div>
            </div>
            <div class="info-item">
                <div class="label">收货地址：</div>
                <div>{{ cartStore.address.detail }}</div>
            </div>
            <div class="info-item">
                <div class="label">收货手机号：</div>
                <div>{{ cartStore.address.phone }}</div>
            </div>
        </div>
    </div>
    <!-- 商品信息 -->
    <h3 class="box-title">商品信息</h3>
    <div class="box-body">
        <table class="goods">
        <thead>
            <tr>
            <th width="520">商品信息</th>
            <th width="170">单价</th>
            <th width="170">数量</th>
            <th width="170">小计</th>
            <th width="170">实付</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="i in cartStore.products" :key="i.id">
            <td>
                <a href="javascript:;" class="info">
                  <img :src="i.image" alt="">
                  <div class="right">
                      <p>{{ i.productName }}</p>
                      <p>{{ i.productSku }}</p>
                  </div>
                </a>
            </td>
            <td>&yen;{{ i.price }}</td>
            <td>{{ i.number }}</td>
            <td>&yen;{{ i.price * i.number }}</td>
            <td>&yen;{{ i.price * i.number }}</td>
            </tr>
        </tbody>
        </table>
    </div>
    <!-- 支付方式 -->
    <h3 class="box-title">支付方式</h3>
    <div class="pay-body">
      <div class="pay-item wx" :class="{ active: payMethod === 1 }" @click="changePayType(1)">
        <img src="@/assets/icon/wx.png" alt="">
      </div>
      <div class="pay-item" style="margin-left: 20px;" :class="{ active: payMethod === 2 }"  @click="changePayType(2)">
        <img src="@/assets/icon/alipay.png" alt="" style="height: 30px;">
      </div>
    </div>
    <!-- 金额明细 -->
    <h3 class="box-title">金额明细</h3>
    <div class="box-body">
        <div class="total">
        <dl>
            <dt>商品件数：</dt>
            <dd>{{ cartStore.products?.length }}件</dd>
        </dl>
        <dl>
            <dt>商品总价：</dt>
            <dd>¥{{ cartStore.totalPrice }}</dd>
        </dl>
        <dl>
            <dt>运<i></i>费：</dt>
            <dd>¥{{ 0.00 }}</dd>
        </dl>
        <dl>
            <dt>应付总额：</dt>
            <dd class="price">{{ cartStore.totalPrice }}</dd>
        </dl>
        </div>
    </div>
    <!-- 提交订单 -->
    <div class="submit">
        <el-button type="primary" size="large" @click="pay">付款</el-button>
    </div>
    </div>
</div>
</div>
</template>

<style scoped lang="scss">
.xtx-pay-checkout-page {
    width: 1280px;
    margin: 20px auto;

    .wrapper {
        background: #fff;
        padding: 0 20px;

        .box-title {
            font-size: 16px;
            font-weight: normal;
            padding-left: 10px;
            line-height: 70px;
            border-bottom: 1px solid #f5f5f5;
        }

        .box-body {
            padding: 20px 0;
        }
    }
}

.pay-body {
  display: flex;
  padding: 20px 0;

  .pay-item {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 140px;
    height: 45px;
    border: 1px solid #d8d8d8;
    cursor: pointer;

    &:hover {
      border-color: #39bf3e;
    }

    img {
      height: 35px;
    }
  }

  .active {
      border-color: #39bf3e;
    }
}

.address-card {
    position: relative;
    display: flex;
    justify-content: space-between;
    width: 100%;
    padding: 15px;
    border-radius: 4px;
    border: 1px dashed #a6a6a6;
    color: #525252;
    font-size: 14px;
    margin-top: 10px;
    margin-bottom: 20px;

    .info-item {
        display: flex;
        margin-bottom: 15px;

        &:last-child {
            margin-bottom: 0;
        }
    }
}

.goods {
  width: 100%;
  border-collapse: collapse;
  border-spacing: 0;

  .info {
    display: flex;
    text-align: left;

    img {
      width: 70px;
      height: 70px;
      margin-right: 20px;
    }

    .right {
      line-height: 24px;

      p {
        &:last-child {
          color: #999;
        }
      }
    }
  }

  tr {
    th {
      background: #f5f5f5;
      font-weight: normal;
    }

    td,
    th {
      text-align: center;
      padding: 20px;
      border-bottom: 1px solid #f5f5f5;

      &:first-child {
        border-left: 1px solid #f5f5f5;
      }

      &:last-child {
        border-right: 1px solid #f5f5f5;
      }
    }
  }
}

.my-btn {
  width: 228px;
  height: 50px;
  border: 1px solid #e4e4e4;
  text-align: center;
  line-height: 48px;
  margin-right: 25px;
  color: #666666;
  display: inline-block;

  &.active,
  &:hover {
    border-color: $xtxColor;
  }
}

.total {
  dl {
    display: flex;
    justify-content: flex-end;
    line-height: 50px;

    dt {
      i {
        display: inline-block;
        width: 2em;
      }
    }

    dd {
      width: 240px;
      text-align: right;
      padding-right: 70px;

      &.price {
        font-size: 20px;
        color: $priceColor;
      }
    }
  }
}

.submit {
  text-align: right;
  padding: 60px;
  border-top: 1px solid #f5f5f5;
}

.addressWrapper {
  max-height: 500px;
  overflow-y: auto;
}

.text {
  flex: 1;
  min-height: 90px;
  display: flex;
  align-items: center;

  &.item {
    border: 1px solid #f5f5f5;
    margin-bottom: 10px;
    cursor: pointer;

    &.active,
    &:hover {
      border-color: $xtxColor;
      background: lighten($xtxColor, 50%);
    }

    >ul {
      padding: 10px;
      font-size: 14px;
      line-height: 30px;
    }
  }
}
</style>